using System;
using System.Collections;
using System.Runtime.Serialization;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using VSSDKAssist.Assets.Helpers;
using VSSDKAssist.Assets.References.Base;

namespace VSSDKAssist.Recipes.Common.References
{
    /// <summary>
    /// UnboundReference for a CSharp VS Package Project
    /// </summary>
    [Serializable]
    public class CSharpVSPackageProjectReference : VSPackageProjectReference
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpVSPackageProjectReference"/> class.
        /// </summary>
        /// <param name="recipe">The recipe.</param>
        public CSharpVSPackageProjectReference(String recipe) : base(recipe) { }
        #endregion

        #region Public Implementation
        /// <summary>
        /// Determines whether the reference is enabled for a particular target item,
        /// based on the condition contained in the reference.
        /// </summary>
        /// <param name="target">The <see cref="T:System.Object"/> to check for references.</param>
        /// <returns>
        /// 	<see langword="true"/> if the reference is enabled for the given <paramref name="target"/>.
        /// Otherwise, <see langword="false"/>.
        /// </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public override bool IsEnabledFor(object target)
        {
            try
            {
                if(base.IsEnabledFor(target))
                {
                    Project project = target as Project;

                    if(project != null && project.CodeModel != null)
                    {
                        return (project.CodeModel.Language.Equals(CodeModelLanguageConstants.vsCMLanguageCSharp));
                    }
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// See <see cref="P:Microsoft.Practices.RecipeFramework.IAssetReference.AppliesTo"/>.
        /// </summary>
        /// <value></value>
        public override String AppliesTo
        {
            get { return Properties.Resources.CSharpVSPackageProjectReference; }
        }
        #endregion

        #region ISerializable Members

        /// <summary>
        /// Required constructor for deserialization.
        /// </summary>
        protected CSharpVSPackageProjectReference(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        #endregion ISerializable Members
    }
}
